/*
Replication files for Media Effects and Mobilization in a Polarized Political System: The Case of Turkey
Ali Carkoglu & Kerem Yildirim 

If necessary:
	1- install grc1leg and balancetable packages
	2- navigate and change directory to the relevant folder, cd ".../replication files"
*/

use "dataset.dta"
	des, short
	*should have 12848 responses and 53 variables, size should be 2,890,800.
	
*create a sample variable that marks all effective observations that will be used in the analyses later on:
	qui: mlogit post i.pre favorakp favorchp favormhp favorhdp favormhp mobil_akp mobil_chp mobil_hdp mobil_mhp i.year
		gen sample= e(sample)
		tab sample
	*will be working with 2101 respondents, rest are non-response or control survey respondents across years
	*note: additional models will include demographic controls which reduces the effective sample in some models
	
*creating a stack for undecided voters:
	replace mobil_undecided=0
	egen fav_undec= rowtotal(favorakp favorchp favorhdp favormhp favorother)
		replace favorundecided= fav_undec/5
	egen vis_undec= rowtotal(visib_akp visib_chp visib_hdp visib_mhp visib_other)
		replace visib_undecided= vis_undec/5
	egen pos_undec= rowtotal(post_akp post_chp post_hdp post_mhp post_other)
		replace post_undecided=1 if pre_undecided!=. & pos_undec==0
		replace post_undecided=0 if pre_undecided!=. & pos_undec==1
		replace post=6 if post_undecided==6
		replace post=6 if post_undecided==1
		replace post_undecided=. if post_akp==.
		replace post=. if post_akp==.

*mark all switchers:
	gen change=1 if pre!=post
	replace change=0 if pre==post & pre!=.
	
*table 1:
	gen akp=.
		replace akp=1 if pre_akp==0 & post_akp==0
		replace akp=2 if pre_akp==1 & post_akp==0
		replace akp=3 if pre_akp==0 & post_akp==1 & pre!=6
		replace akp=3 if pre==6 & post_akp==1
		replace akp=4 if pre_akp==1 & post_akp==1
	gen chp=.
		replace chp=1 if pre_chp==0 & post_chp==0
		replace chp=2 if pre_chp==1 & post_chp==0
		replace chp=3 if pre_chp==0 & post_chp==1 & pre!=6
		replace chp=3 if pre==6 & post_chp==1
		replace chp=4 if pre_chp==1 & post_chp==1

	label define switching 1 "No support pre and post" 2 "Switched from party in post" ///
		3 "Switched to party in post" 4 "Support in both pre and post", replace
		label values akp chp switching

	gen mark=1 if akp!=. & favorakp!=. & chp!=. & favorchp!=.
		tab akp year if mark==1, nofreq col
			table akp year if mark==1, c(mean favorakp)
		tab chp year if mark==1, nofreq col
			table chp year if mark==1, c(mean favorchp)
	*number of undecided voters who switched to akp or chp is 40% of all undecided
			tab akp if pre==6,
			tab chp if pre==6
			di (114+346)/1125
	*number of voters reported preference for akp or chp and then switched to another party is 16.2%
			tab akp 
			tab chp
			di (488+322)/4985
	drop chp mark akp

*note that data was previously in long format and needs to be in the long format for regression analyses:
	reshape long
		encode ehe, gen(party)


*table 2, with additional demographic controls in the appendix:
	logit post_ c.favor pre_ gender age urban kurd educ income_std if sample==1 & ehe!="undecided", cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo m1_2, title(Model 1)
	
	logit post_ c.favor c.visib i.undecided pre_ i.year i.party gender age urban kurd educ income_std if sample==1 & ehe!="undecided", cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo m2_2, title(Model 2)
		
	logit post_ c.favor c.visib i.undecided pre_ mobil_ i.year i.party gender age urban kurd educ income_std if sample==1 & ehe!="undecided", cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo m3_2, title(Model 3)
	 
	logit post_ c.favor#c.visib pre_ i.undecided mobil_ i.year i.party gender age urban kurd educ income_std if sample==1 & ehe!="undecided", cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo m4_2, title(Model 4)

		esttab m1_2 m2_2 m3_2 m4_2 using table2_2.rtf, star(+ 0.10 * 0.05 ** 0.010 *** 0.001) cells(b(star fmt(%9.3f)) se(par)) stats(nagelkerke corr N N_clust, ///
		fmt(%9.3f %9.2f %9.0f %9.0f)    labels("Nagelkerke r2" "% Correctly classified" "Voter-Party Dyads" "N")) ///
		legend label collabels(none) varlabels(_cons Constant pre_ "Pre" 1.undecided "Undecided" ///
		visib_ "Visibility" c.favor#c.visib_ "Fav x Vis" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.party "CHP" 3.party "Kurdish Parties" 4.party "MHP" 5.party "Others" ///
		gender "Female" age "Age" urban "Urban" kurd "Kurdish" educ "Education" income_std "Income") ///
		order(favor visib_ c.favor#c.visib_ mobil_ pre_ 1.undecided) drop(0.undecided 1.party 2002.year) replace ///
		mtitles ("(1)" "(2)" "(3)" "(4)") nodepvars nonumbers ///
		addnotes("Logistic regression, DV= Post-election survey reported vote choice" ///
		"Standard errors are reported in parentheses, clustered by individuals." ///
		"2002 and AKP are reference categories.") ///
		title ("Table 2: Estimating Vote Choice (Stacked Dataset)")
		
	
*table 3 with controls:
		logit post_ c.favor##c.visib pre_ i.undecided mobil_ i.year i.party gender age urban kurd educ income_std if sample==1, cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo m5, title(Model 5)
		
	logit post_ c.favor##c.visib i.undecided mobil_ i.year i.party gender age urban kurd educ income_std if sample==1 & change==1, cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo m6, title(Model 6)
		
	esttab m5 m6 using table3.rtf, star(+ 0.10 * 0.05 ** 0.010 *** 0.001) cells(b(star fmt(%9.3f)) se(par)) stats(nagelkerke corr N N_clust, ///
		fmt(%9.3f %9.2f %9.0f %9.0f) labels("Nagelkerke r2" "% Correctly classified" "Voter-Party Dyads" "N")) ///
		legend label collabels(none) varlabels(_cons Constant pre_ "Pre" 1.undecided "Undecided" ///
		visib_ "Visibility" c.favor#c.visib_ "Fav x Vis" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.party "CHP" 3.party "Kurdish Parties" 4.party "MHP" 5.party "Others" ///
		gender "Female" age "Age" urban "Urban" kurd "Kurdish" educ "Education" income_std "Income") ///
		order(favor visib_ c.favor#c.visib_ mobil_ pre_ 1.undecided) drop(0.undecided 1.party 2002.year 6.party) replace ///
		mtitles ("Model 5 Switchers and Partisans" "Model 6 Only Switchers") nodepvars nonumbers ///
		addnotes("Standard errors are reported in parentheses, clustered by individuals." ///
		"2002 and AKP are reference categories.") ///
		title ("Table 3: Full model specification (Model 5) with a comparative subsample (Model 6)")
		
		
*figure 1 - based on Model 5:
	logit post_ c.favor##c.visib pre_ i.undecided mobil_ i.year i.party gender age urban kurd educ income_std if sample==1, cluster(id_year)
		sum favor if e(sample), d
		sum visib_ if e(sample), d
			di r(p10) r(p90)
			*use these values for p10 and p90 in predictions:
	margins, at(favor=(-1(0.25)0.75) visib_=(.0166185 .32059589))
	marginsplot, title("") ytitle("Likelihood to support the party") ///
		legend(region(fcolor(none)) position(5) ring(0) title("Visibility") ///
		order(1 "10 Percentile" 2 "90 Percentile")) ///
		plotregion(fcolor(none) ifcolor(none)) ///
		yscale(range(0 0.4)) ylabel(0(.1)0.4) xlabel(, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
		recastci(rarea) ci1opts(color(plr1%30))  ci2opts(color(plb1%30)) ///
		plot1opts(color(plr1))  plot2opts(color(plb1)) 
 
*figure 2 - based on Model 6:
	logit post_ c.favor##c.visib i.undecided mobil_ i.year i.party gender age urban kurd educ income_std if sample==1 & change==1, cluster(id_year)
		sum favor if e(sample), d
		sum visib_ if e(sample), d
			di r(p10) r(p90)
			*use these values for p10 and p90 in predictions:
	margins, at(favor=(-1(0.25)0.75) visib_=(.0155082 .3142398))
	marginsplot, title("") ytitle("Likelihood to support the party") ///
		legend(region(fcolor(none)) position(5) ring(0) title("Visibility") ///
		order(1 "10 Percentile" 2 "90 Percentile")) ///
		plotregion(fcolor(none) ifcolor(none)) ///
		yscale(range(0 0.4)) ylabel(0(.1)0.4) xlabel(, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
		recastci(rarea) ci1opts(color(plr1%30))  ci2opts(color(plb1%30)) ///
		plot1opts(color(plr1))  plot2opts(color(plb1)) 

*figure 3, figure a.4 and table a.7 in the appendix:

	*table a.7 (Model 1):
		logit post_ c.favor##c.visib##i.change pre_ i.undecided mobil_ i.year i.party gender age urban kurd educ income_std if sample==1, cluster(id_year)
			estadd nagelkerke
			estat class
			estadd scalar corr= r(P_corr)
			eststo a4
			
		esttab a4 using a4.rtf, cells(b(star fmt(%9.3f)) se(par)) stats(nagelkerke corr N N_clust, ///
		fmt(%9.3f %9.2f %9.0f %9.0f) labels("Nagelkerke r2" "% Correctly classified" "Voter-Party Dyads" "N")) ///
		legend label collabels(none) varlabels(_cons Constant pre_ "Pre" 1.undecided "Undecided" ///
		visib_ "Visibility" c.favor#c.visib_ "Fav x Vis" 2007.year "2007" 2011.year "2011" ///
		1.change "Switcher" 1.change#c.favor "Fav x Switcher" 1.change#c.visib_ "Vis x Switcher" ///
		1.change#c.favor#c.visib_ "Fav x Switcher x Vis" ///
		2015.year "2015" 2.party "CHP" 3.party "Kurdish Parties" 4.party "MHP" 5.party "Others" ///
		gender "Female" age "Age" urban "Urban" kurd "Kurdish" educ "Education" income_std "Income") ///
		order(favor visib_ c.favor#c.visib_ 1.change 1.change#c.favor 1.change#c.visib_ 1.change#c.favor#c.visib_ ///
		mobil_ pre_ 1.undecided) ///
		drop(0.undecided 1.party 2002.year 6.party 0.change*) replace ///
		nomtitles nodepvars nonumbers ///
		addnotes("Standard errors are reported in parentheses, clustered by individuals." ///
		"2002 and AKP are reference categories.") ///
		title ("Table A.4: Three-Way interaction model)")
	
	*figure 3:
		sum visib_ if e(sample)==1 & change==0, d
		di r(p10) r(p90)
			*use these values for p10 and p90 in predictions for non-switchers:
		qui: margins, at(favor=(-1(0.25)0.75) visib_=(.0166185 .32139599) change=(0))
		marginsplot, title("Party supporters (same in both pre & post") ///
			ytitle("") ///
			legend(region(fcolor(none)) position(5) ring(0) title("Visibility") ///
			order(1 "10 Percentile" 2 "90 Percentile")) ///
			plotregion(fcolor(none) ifcolor(none)) ///
			yscale(range(0 0.4)) ylabel(0(.1)0.4) xlabel(, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
			recastci(rarea) ci1opts(color(plr1%30))  ci2opts(color(plb1%30)) ///
			plot1opts(color(plr1))  plot2opts(color(plb1))  name(remainers, replace)  
		
		
		sum visib_ if e(sample)==1 & change==1, d
		di r(p10) r(p90)
			*use these values for p10 and p90 in predictions for switchers:
		qui: margins, at(favor=(-1(0.25)0.75) visib_=(.0155082 .3142398) change=(1))
		marginsplot, title("Switchers") ///
			ytitle("Likelihood to support the party") ///
			legend(region(fcolor(none)) cols(2) position(5) ring(0) title("Visibility") ///
			order(1 "10 Percentile" 2 "90 Percentile")) ///
			plotregion(fcolor(none) ifcolor(none)) ///
			yscale(range(0 0.4)) ylabel(0(.1)0.4) xlabel(, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
			recastci(rarea) ci1opts(color(plr1%30))  ci2opts(color(plb1%30)) ///
			plot1opts(color(plr1))  plot2opts(color(plb1)) name(switchers, replace) 
		
		grc1leg switchers remainers, legendfrom(switchers)
		
		*figure a.4 marginal effects of being a switcher:
		margins, dydx(change) at(favor=(-1(0.25)0.75) visib_=(.0155082 .3142398))
		marginsplot, title("") ///
			ytitle("Marginal Effect of Being a Switcher") ///
			legend(region(fcolor(none)) cols(1) position(7) ring(0) title("Visibility") ///
			order(1 "10 Percentile" 2 "90 Percentile")) ///
			plotregion(fcolor(none) ifcolor(none)) ///
			yscale(range(-0.2 0.2)) ylabel(-0.2(.1)0.2) xlabel(, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
			recastci(rarea) ci1opts(color(plr1%30))  ci2opts(color(plb1%30)) ///
			plot1opts(color(plr1))  plot2opts(color(plb1)) name(switchers, replace) 
			
		
*Who drives the media effect? Undecided voters vs. Party switchers (Table A.7 Model 2):
		logit post_ c.favor##c.visib##i.undecided pre_ mobil_ i.year i.party gender age urban kurd educ income_std if sample==1 & change==1, cluster(id_year)
			estadd nagelkerke
			estat class
			estadd scalar corr= r(P_corr)
			eststo a4_1
	
		esttab a4_1 using a4_1.rtf, cells(b(star fmt(%9.3f)) se(par)) stats(nagelkerke corr N N_clust, ///
		fmt(%9.3f %9.2f %9.0f %9.0f) labels("Nagelkerke r2" "% Correctly classified" "Voter-Party Dyads" "N")) ///
		legend label collabels(none) varlabels(_cons Constant pre_ "Pre" 1.undecided "Undecided" ///
		visib_ "Visibility" c.favor#c.visib_ "Fav x Vis" 2007.year "2007" 2011.year "2011" ///
		1.undecided "Switcher" 1.undecided#c.favor "Fav x Undecided" 1.undecided#c.visib_ "Vis x Undecided" ///
		1.undecided#c.favor#c.visib_ "Fav x Undecided x Vis" ///
		2015.year "2015" 2.party "CHP" 3.party "Kurdish Parties" 4.party "MHP" 5.party "Others" ///
		gender "Female" age "Age" urban "Urban" kurd "Kurdish" educ "Education" income_std "Income") ///
		order(favor visib_ c.favor#c.visib_ 1.undecided 1.undecided#c.favor 1.undecided#c.visib_ 1.undecided#c.favor#c.visib_ ///
		mobil_ pre_ ) ///
		drop(0.undecided* 1.party 2002.year) replace ///
		nomtitles nodepvars nonumbers ///
		addnotes("Standard errors are reported in parentheses, clustered by individuals." ///
		"2002 and AKP are reference categories.") ///
		title ("Table A.4-2: Three-Way interaction model within the Switchers Subsample)")
		
*table a.1 - summary statistics:
	label list party
	foreach var of varlist post_ pre_ favor visib_ mobil_  {
		foreach num of numlist 1/5 {
			sum `var' if party==`num' & sample==1
		}
	}

*table a.3 - total and effective sample size across TES surveys
	gen panel=1 if pre!=. & post!=. & post!=6
		replace panel=0 if panel==.
	gen effective=1 if sample==1
		replace effective=0 if effective==.
	gen total=1
	table year, c(sum total sum panel sum effective) row
	

*balance tests, tables a.4 to a.6:
preserve
clear
use "dataset.dta"

	gen reader=0 if type=="Does not read"
	replace reader=1 if type!="Does not read"
	gen change=1 if pre!=post
	replace change=0 if pre==post & pre!=.
	gen newspapers_15=0 if type=="Reads other newspaper"
	replace newspapers_15=1 if type!="Reads other newspaper" & type!= "Does not read"
	replace newspapers_15=2 if type=="Does not read"
	gen newspaper_15_2= newspapers_15
	recode newspaper_15_2 (2=.)
	gen newspaper_15_3= newspapers_15
	recode newspaper_15_3 (0=.) (2=0)

	*table a.4
	balancetable reader gender age urban kurd  leftright educ religiosity income_std using "balance.xlsx",replace
	*table a.5
	balancetable panel reader gender age urban kurd  leftright educ religiosity income_std using "balance2.xlsx" if group!=-1, replace
	*table a.6 part 1:
	balancetable newspaper_15_2 gender age urban kurd  leftright educ religiosity  income_std using "balance5_2.xlsx" if  panel==1, replace
	*table a.6 part 2:
	balancetable newspaper_15_3 gender age urban kurd  leftright educ religiosity  income_std using "balance5_3.xlsx" if  panel==1, replace	

restore
	
*** note: table a.2, figure a.1, figure a.2 figure a.3, figure a.8 are based on raw content data.
*** Dataset will become available with a later publication. Contact authors for details.



*table a.9:
*were switchers more likely to be influenced by favorabiltiy and visibility:
	gen switcher=1 if pre!=post
	replace switcher=0 if pre==post
	replace switcher=. if post==. | pre==.

	logit switcher c.favor##c.visib mobil_ i.year i.party  if sample==1, cluster(id_year)
		estadd nagelkerke
		estat class
		estadd scalar corr= r(P_corr)
		eststo a6
			
	esttab a6 using a6.rtf, cells(b(star fmt(%9.3f)) se(par)) stats(nagelkerke corr N N_clust, ///
		fmt(%9.3f %9.2f %9.0f %9.0f) labels("Nagelkerke r2" "% Correctly classified" "Voter-Party Dyads" "N")) ///
		legend label collabels(none) varlabels(_cons Constant 2.pre "CHP-pre" 3.pre "MHP-pre" 4.pre "Kurdish-pre" ///
		5.pre "Other-pre" 6.pre "Undecided"  visib_ "Visibility" c.favor#c.visib_ "Fav x Vis" 2007.year "2007" ///
		2011.year "2011" 2015.year "2015" 2.party "CHP-post" 3.party "Kurdish Parties-post" 4.party "MHP-post") /// 
		order(favor visib_ c.favor#c.visib_) ///
		drop(1.party 2002.year) replace ///
		nomtitles nodepvars nonumbers ///
		addnotes("Standard errors are reported in parentheses, clustered by individuals." ///
		"Reference categories: 2002, AKP in pre election, AKP in post election") ///
		title ("Table A.5: Likelihood to Switch to Different Parties")

	*figure a.5:	
	sum visib if e(sample)==1, d
	di r(p10) r(p90)
	qui: margins, at(favor=(-1 (0.25) 1) visib_=(.0206 .31359389))
		marginsplot, title("") ytitle("Likelihood to switch") ///
		legend(region(fcolor(none)) position(5) ring(0) title("Visibility") ///
		order(1 "10 Percentile" 2 "90 Percentile")) ///
		plotregion(fcolor(none) ifcolor(none)) ///
		yscale(range(0 0.6)) ylabel(0(.1)0.6) xlabel(, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
		recastci(rarea) ci1opts(color(plr1%30))  ci2opts(color(plb1%30)) ///
		plot1opts(color(plr1))  plot2opts(color(plb1)) 

*table a.10:
table newspaper_choice year, c(mean mainstream)

*table a.11:
	logit mainstream i.change##i.pre i.gender age i.urban income_std educ kurd, cluster(id_year)
			estadd nagelkerke
			estat class
			estadd scalar corr= r(P_corr)
			eststo a8
			
	esttab a8 using a8.csv, cells(b(star fmt(%9.3f)) se(par)) stats(nagelkerke corr N N_clust, ///
		fmt(%9.3f %9.2f %9.0f %9.0f) labels("Nagelkerke r2" "% Correctly classified" "Voter-Party Dyads" "N")) ///
		legend label collabels(none) varlabels(_cons Constant 1.change "Switcher" 2.pre "CHP-pre" 3.pre "MHP-pre" 4.pre "Kurdish-pre" ///
		5.pre "Other-pre" 6.pre "Undecided" educ "Education (years)" age "Age" 1.gender "Female" 1.urban "Urban" ///
		income_std "Income" 1.change#2.pre "Switched from CHP" 1.change#3.pre "Switched from MHP" 1.change#4.pre "Switched from Kurdish" ///
		1.change#5.pre "Switched from Other" 1.change#6.pre "Switched from Undecided" ) ///
		order(1.change ) ///
		drop(1.pre 0.change 0.gender 0.urban 0.change* *#1.pre) replace ///
		nomtitles nodepvars nonumbers ///
		addnotes("Standard errors are reported in parentheses, clustered by individuals." ///
		"Reference categories: Partisan Loyalists, AKP in pre election, Males, Rural areas") ///
		title ("Table A.5: Likelihood to Switch to Different Parties")
		
		*figure a.6
		margins change#pre
		marginsplot, xdimension(pre) ytitle("") xtitle("Pre-election party preference") ///
		title("Likelihood to read Mainstream paper") ///
		legend(region(fcolor(none)) position(5) ring(0) title("") ///
		order(1 "Partisan" 2 "Switcher")) ///
		plotregion(fcolor(none) ifcolor(none)) ///
		yscale(range(0.4 0.9)) ylabel(0.4(.1)0.9) /// 
		xlabel(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish" 5 "Other" 6 "Undecided", labsize(vsmall) labcolor(black) ///
		glcolor(gs12)) ylabel(, glcolor(gs12)) ///
		recastci(rcap) ci1opts(color(plr1%50))  ci2opts(color(plb1%50)) ///
		recast (scatter) plot1opts(color(plr1))  plot2opts(color(plb1))  ciopts(lwidth(thick))
		

